package com.xvdong.audioplayer.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.xvdong.audioplayer.R;
import com.xvdong.audioplayer.databinding.ItemSearchHistoryBinding;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by xvDong on 2024/5/9.
 */

public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder> {

    private List<String> mHistory;
    private onItemClickListener mListener;

    public SearchHistoryAdapter(List<String> history , onItemClickListener listener) {
        mHistory = history;
        mListener = listener;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSearchHistoryBinding binding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_search_history, parent, false);
        return new SearchHistoryAdapter.ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchHistoryAdapter.ViewHolder holder, int position) {
        holder.mBinding.tvHistory.setOnClickListener(v -> {
            if (mListener != null){
                mListener.onItemClick(mHistory.get(position));
            }
        });
        holder.bind(mHistory.get(position));
    }

    @Override
    public int getItemCount() {
        return mHistory.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemSearchHistoryBinding mBinding;

        public ViewHolder(@NonNull ItemSearchHistoryBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bind(String history) {
            mBinding.tvHistory.setText(history);
        }
    }

    public interface onItemClickListener{
        void onItemClick(String history);
    }
}
